<!-- 下载页面 -->
{include 'admin/layout/header_list'}
<link rel="stylesheet" href="__ADMIN_STATIC__/css/pop_window.css?random={extendSetting('random')}">
<div class="downLoadList" style="margin: 20px;">
    <h4>下载列表</h4>
    <div class="downLoadListTable">
        <table>
            <col>
            <col width="120px">
            <col width="120px">
            <col width="120px">
            <tr class="downloadList-item">
                <td class="th"><span>文件名</span></td>
                <td class="th"><span>文件大小</span></td>
                <td class="th"><span>下载速度</span></td>
                <td class="th"><span>下载进度</span></td>
            </tr>
            <tr class="downloadList-item">
                <td>
                    <span>{basename($rowData['downUrl'])}</span>
                    <input class="file" type="hidden" data-filesize="0" data-isover="0" data-exec="0" data-nums="0" data-plugin_id="{$rowData['id']}" value="{urlencode($rowData['downUrl'])}" />
                </td>
                <td>{$rowData['fileSizeFormat']}</td>
                <td><span class="rate">---</span></td>
                <td><span class="progress">0%</span></td>
            </tr>
        </table>
    </div>
</div>
<input type="hidden" class="installId" value="{if isset($request['pluginId'])}{$request['pluginId']}{/if}" />
<input type="hidden" class="goodId" value="{if isset($request['goodId'])}{$request['goodId']}{/if}" />
<script>
    $(function (){
        download();
    });

    /**
     * 检测是否可进行安装
     */
    function checkInstall(){
        //检测安装
        var install=1;
        $(".downLoadList").find(".file").each(function (index,ele){
            var isover=$(ele).data("isover");
            if(isover=="0"){
                install=0;
            }
        });
        if(install==1){
            //执行安装
            var installId=$(".installId").val();
            var goodId=$(".goodId").val();
            window.location.href="{LTCMS_ROUTE_PREFIX}/plugin/install?pluginId="+installId+"&goodId="+goodId;
        }
    }

    function download (){
        //检测下载列表，没下载完的继续下载,同时下载几个
        var process_nums=2;
        var execNums=0;
        var url="{LTCMS_ROUTE_PREFIX}/plugin/downloadFile";
        $(".downLoadList").find(".file").each(function (index,ele){
            var isOver=$(ele).data("isover");
            var exec=$(ele).data("exec");
            var val=$(ele).val();
            if(execNums <= process_nums){
                if(isOver=="0" && exec=="0"){
                    //计数加1
                    execNums+=1;

                    //进行下载
                    var downUrl=$(ele).val();
                    var fileSize=$(ele).data("filesize");
                    var split_nums=$(ele).data("nums");
                    var plugin_id=$(ele).data("plugin_id");
                    $(ele).data("exec","1");

                    $.ajax({
                        processData:false,
                        contentType :false,
                        type: "get",
                        url: url+"?downUrl="+downUrl+"&num="+split_nums+"&fileSize="+fileSize+"&pluginId="+plugin_id,
                        async: true,
                        dataType: "jsonp",
                        timeout: 0,
                        success:function(data)
                        {
                            var conError=1;
                            if(typeof data == "undefined")
                            {
                                conError=2;
                            }
                            else
                            {
                                if(!(typeof data == "object"))
                                {
                                    data=eval("("+data+")");
                                }
                            }
                            if(data.success){
                                var nums=$(ele).data("nums");
                                $(ele).data("nums",nums+1);
                                $(ele).data("filesize",data.data.fileSize);
                                $(ele).parents(".downloadList-item").find(".progress").html(data.data.progress);
                                $(ele).parents(".downloadList-item").find(".rate").html(data.data.rate);

                                if(data.data.isOver == "1"){
                                    $(ele).data("isover","1");
                                }
                            }else{
                                //下载失败
                                $(ele).parents(".downloadList-item").find(".progress").html("失败");
                                $(ele).data("isover","1");
                            }
                        },
                        complete:function(xhr)
                        {
                            $(ele).data("exec","0");
                            execNums-=1;
                            download();
                        }
                    });
                }
            }
        });
        checkInstall(); //检测是否安装
    }
</script>
{include 'admin/layout/footer'}